using System;
using System.Collections.Generic;
using System.Text;
using DirectMidi;
//using System.Threading;

namespace NetMonitor
{
    public class Sound
    {
        // midi objects
        CDirectMusic CDMusic = new CDirectMusic();
        COutputPort COutPort = new COutputPort();
        CDLSLoader CLoader = new CDLSLoader();
        CCollection CCollection;
        CInstrument ins;

        public Sound()
        {
            initSound();
        }

        private void initSound()
        {
            CDMusic.Initialize();
            // Initialize ports given the DirectMusic manager object
            COutPort.Initialize(CDMusic);


            INFOPORT PortInfo;
            UInt32 dwPortCount = 0;

            // Software Synthesizer selection
            do
                COutPort.GetPortInfo(++dwPortCount, out PortInfo);
            while (0 == (PortInfo.dwFlags & DMUS_PC.SOFTWARESYNTH));
            do
                COutPort.GetPortInfo(++dwPortCount, out PortInfo);
            while (0 == (PortInfo.dwFlags & DMUS_PC.SOFTWARESYNTH));
            // Output port activation given the port information 
            COutPort.SetPortParams(0, 2, 2, SET.REVERB | SET.CHORUS, 44100);
           
            COutPort.ActivatePort(PortInfo);
            Console.WriteLine("Selected output port: " + PortInfo.szPortDescription);

            // Initialize the Loader object  
            CLoader.Initialize();

            // Loads the deafault GM collection of the software synthesizer
            CLoader.LoadDLS(null, out CCollection);

            // Structure of the instrument information
            loadInstrument(CCollection, 1, 0);
        }

        private void loadInstrument(CCollection _coll, int _index, uint _patch)
        {
            
            _coll.GetInstrument(out ins, _index);
            ins.SetPatch(_patch);
            ins.SetNoteRange(0, 127);

            COutPort.DownloadInstrument(ins);
        }

        public void noteOn(byte _note)
        {
            // lock (COutPort)
            SYNTHSTATS ss;
           // DMUS_SYNTHSTATS. dss;
            COutPort.GetSynthStats(out ss);
          //  dss = ss.dwValidStats;
       
            Console.WriteLine(" voices " + ss.dwVoices.ToString());
           {

                int ret = 0;
                if (_note > 40 && _note < 70)
                {
                   // COutPort.SendMidiMsg(COutputPort.EncodeMidiMsg(MidiMessage.NOTE_OFF, 0, _note, 100), 0);
                    ret = COutPort.SendMidiMsg(COutputPort.EncodeMidiMsg(MidiMessage.NOTE_ON, 0, _note, 100), 0);
                    Console.WriteLine("on " + _note.ToString() + " ret = " + ret.ToString());
                }
            }
        }

        public void noteOff(byte _note)
        {
            //  lock (COutPort)
            {

                int ret = 0;
                if (_note > 40 && _note < 50)
                {
                    COutPort.SendMidiMsg(COutputPort.EncodeMidiMsg(MidiMessage.NOTE_OFF, 0, _note, 100), 0);
                    Console.WriteLine("off " + _note.ToString() + " ret = " + ret.ToString());
                }
            }
        }
    }
}
